home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / shells / rc-1.000 / rc-1 / rc-1.5-linux / b_read.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-22  |  1.4 KB  |  60 lines

  1. /* 
  2. Here are both external and built-in implementations of read, following v7
  3. semantics on EOF (i.e., set the variable to '' and return 1), and a trip.read.
  4.  
  5.  b_read -- read a single line, terminated by \n or EOF, from the standard
  6.  input, and assign the line without the terminator to av[1].
  7. */
  8. #include "proto.h"
  9. #include <stdio.h>
  10. #include "addon.h"
  11. typedef enum bool { FALSE, TRUE } bool;
  12. extern void set(bool);
  13.  
  14. static int readchar (int fd) {
  15.    unsigned char c;
  16.    if (read (fd, &c, 1) == 1) return c;
  17.    else return EOF;
  18. }
  19.  
  20. void b_read (char **av) {
  21.    char *name;
  22.    int c;
  23.    char *line;
  24.    SIZE_T len;
  25.    SIZE_T max_len;
  26.    SIZE_T max_len_quantum;
  27.  
  28. /* check usage is "read name" */
  29.    if (av[1] == NULL)
  30.       rc_error ("missing variable name");
  31.    if (av[2] != NULL)
  32.       rc_error ("too many arguments to read");
  33.    name = av[1];
  34.  
  35. /* read a single line from stdin */
  36.    line = NULL;
  37.    len = 0;
  38.    max_len = 0;
  39.    max_len_quantum = 256;
  40.    do {
  41.       c = readchar (0);
  42.       if (len == max_len) 
  43.      line = (char*) erealloc (line, max_len += max_len_quantum);
  44.       if (c == '\n' || c == EOF)
  45.      line[len] = 0;
  46.       else
  47.      line[len] = c;
  48.       len++;
  49.    } while (c != '\n' && c != EOF);
  50.  
  51. /* assign whatever we read to the variable */
  52.    assign (word (name, NULL), word (line, NULL), FALSE);
  53.    efree (line);
  54.  
  55. /* return TRUE if we terminated with a \n, otherwise FALSE */
  56.    set (c == '\n');
  57.  
  58.    return;
  59. }
  60.